//
//  UIActivityIndicatorViewControl.swift
//  DemoUI
// 活动指示器控件
//  Created by 白瑾浩 on 2022/1/7.
//

import Foundation
import UIKit

class UIActivityIndicatorViewControl : UIViewController,UIPickerViewDelegate,UIPickerViewDataSource{
   
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        //加载进度条
        let activity = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.large)
        activity.center = self.view.center
        activity.color = UIColor.red //颜色设置 默认灰色
        activity.startAnimating()
        self.view.addSubview( activity )
        
        
        //进度条控制 UIProcessView
        let mUIProcessView = UIProgressView(progressViewStyle: UIProgressView.Style.default)
        //设置位置和宽度
        mUIProcessView.frame = CGRect(x: 20, y: 100, width: 280, height: 10)
        mUIProcessView.progress = 0.5 //设置当前进度
        //设置已经走过的进度条的颜色
        mUIProcessView.progressTintColor = UIColor.green
        //设置未走过的进度条颜色
        mUIProcessView.trackTintColor = UIColor.red
        
        //或者设置图标
        //已经走过的图标
        mUIProcessView.progressImage = UIImage(named: "bird1")
        //未走过的图标
        mUIProcessView.trackImage = UIImage(named: "bird2")
        //添加当前试图
        self.view.addSubview( mUIProcessView )
        
        
        //步进器控件 UIStepper
        let mUIStepper = UIStepper(frame: CGRect(x: 20, y: 140, width: 200, height: 30))
        mUIStepper.tintColor = UIColor.red
        mUIStepper.minimumValue = 0
        mUIStepper.maximumValue = 10
        mUIStepper.stepValue = 1 //步长
        mUIStepper.isContinuous = false //是否按住不放也不断触发
        mUIStepper.autorepeat = false //值是否不断叠加  按住不放
        mUIStepper.wraps = false  //首尾值 是不是不断循环  达到最大值 然后到最小值
        mUIStepper.addTarget(self, action: #selector(UIStepperChange), for: UIControl.Event.valueChanged)
                
        //设置背景图片
        mUIStepper.setBackgroundImage(UIImage(named: "bird1"), for: UIControl.State.normal)
        //设置控件分割线图片
        mUIStepper.setDividerImage(UIImage(named: "bird2"), forLeftSegmentState: UIControl.State.normal,rightSegmentState:UIControl.State.normal)
        
        //设置加号按钮图片
        mUIStepper.setIncrementImage(UIImage(named: "bird3"), for: UIControl.State.normal)
        //设置减号按钮图片
        mUIStepper.setDecrementImage(UIImage(named: "bird4"), for: UIControl.State.normal)
        
        self.view.addSubview( mUIStepper )
        
        //选择器控件  UIPickerView
        let pickerView = UIPickerView(frame: CGRect(x: 20, y: 180, width: 280, height: 200))
        
        pickerView.delegate = self //设置代理
        pickerView.dataSource = self //设置数据源
        
        self.view.addSubview( pickerView )
        
        
        //时间选择器控件 UIDataPicker
        
        let dataPicker = UIDatePicker(frame: CGRect(x: 10, y: 400, width: 300, height: 30))
        //设置时间模式  时间  年月 等
        dataPicker.datePickerMode = UIDatePicker.Mode.countDownTimer
        
        dataPicker.addTarget(self, action:#selector(selectDate), for: UIControl.Event.valueChanged)
        //将控件添加到当前试图
        self.view.addSubview( dataPicker )
        
        
        
        
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        //返回指示器控件的组数
        return 2
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        //返回每个分组行数
        return 10
    }
//    //默认文本1
//    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//        return "行：\(row),组\(component+1)"
//    }
//    //对文本进行自定义控制2
//    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
//        let attri = NSMutableAttributedString(string: "第\(component+1)组\(row)行")
//        attri.addAttributes( [NSAttributedString.Key.foregroundColor:UIColor.red], range: NSRange(location: 0, length: attri.length  ))
//        return attri
//    }
    //自定义内容试图3
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let mUIImage = UIImageView( image: UIImage(named: "bird2") )
        mUIImage.frame = CGRect(x: 0, y: 0, width: 110, height: 30)
        return mUIImage
    }
    //获取用户选择了那一条数据
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        print( "行：\(row),组：\(component+1)" )
    }
    //自定义各个组的组宽
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        if component == 0 {
           return 180
        }else{
            return 100
        }
    }
    //自定义各个组的行高
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        if component == 0 {
           return 30
        }else{
            return 20
        }
    }
    
    
    //进步器的值
    @objc func UIStepperChange(_ UIStepperStr : UIStepper ){
        print( "当前值：",UIStepperStr.value )
        
    }
    //时间代理
    @objc func selectDate(_ dataPicker : UIDatePicker ){
        print( "\(dataPicker.date), \(dataPicker.countDownDuration)" )
    }
    
    
    
}
